package com.happy.zebra.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.happy.zebra.admin.api.entity.SysRole;
import com.happy.zebra.admin.mapper.SysRoleMapper;
import com.happy.zebra.admin.service.SysRoleMenuService;
import com.happy.zebra.admin.service.SysRoleService;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author npc
 * @date 2017/11/5
 */
@RestController
@AllArgsConstructor
@RequestMapping("/role")
@Api(value = "role", description = "角色管理模块")
public class RoleController {

	private final SysRoleService sysRoleService;
	private final SysRoleMenuService sysRoleMenuService;
	private SysRoleMapper roleMapper;


	@GetMapping("/getRoleListByUsername")
	public R getRoleListByUsername(Integer id, String username) {
		List<SysRole> roleList = sysRoleService.findRolesByUserId(id);
		List<Map<String, String>> result = new ArrayList();
		//之后如果有运营商在此处添加逻辑
		for (SysRole item : roleList) {
			//如果有机构身份,则插入机构名称和id
			if (item.getRoleId() == 19) {
				//目前一个手机号只支持建立一个运营商和机构
				List<Map<String, String>> orgList = roleMapper.getOrgByUsername(username);

				for (Map<String, String> org : orgList) {
					Map<String, String> map = new HashMap<>();
					if (null != org) {
						map.put("roleId", "19");
						map.put("name", org.get("name"));
						map.put("orgId", String.valueOf(org.get("id")));
						map.put("type", "org");
						map.put("status", org.get("status"));
						map.put("isStudio", org.get("is_studio"));
						result.add(map);
					}
				}
			}
			//如果有运营商身份, 则插入运营商名称和id
			if (item.getRoleId() == 18) {
				//目前一个手机号只支持建立一个运营商和机构
				List<Map<String, String>> optList = roleMapper.getOptByUsername(username);

				for (Map<String, String> opt : optList) {
					Map<String, String> map = new HashMap<>();
					if (null != opt) {
						map.put("roleId", "18");
						map.put("name", opt.get("name"));
						map.put("optId", String.valueOf(opt.get("id")));
						map.put("type", "opt");
						map.put("rank", opt.get("rank"));
						map.put("status", opt.get("status"));
						map.put("isStudio", "0");
						result.add(map);
					}
				}
			}
			if (item.getRoleId() == 25) {
				//获取经销商信息
				List<Map<String, String>> distList = roleMapper.getDistributorByUsername(username);

				for (Map<String, String> dist : distList) {
					Map<String, String> map = new HashMap<>();
					if (null != dist) {
						map.put("roleId", "25");
						map.put("name", dist.get("name"));
						map.put("distId", String.valueOf(dist.get("id")));
						map.put("type", "dist");
						map.put("status", dist.get("status"));
						map.put("isStudio", "0");
						result.add(map);
					}
				}
			}
		}
		return new R<>(result);
	}

	/**
	 * 通过ID查询角色信息
	 *
	 * @param id ID
	 * @return 角色信息
	 */
	@GetMapping("/{id}")
	public R getById(@PathVariable Integer id) {
		return new R<>(sysRoleService.getById(id));
	}

	/**
	 * 添加角色
	 *
	 * @param sysRole 角色信息
	 * @return success、false
	 */
	@SysLog("添加角色")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_role_add')")
	public R save(@Valid @RequestBody SysRole sysRole) {
		return new R<>(sysRoleService.save(sysRole));
	}

	/**
	 * 修改角色
	 *
	 * @param sysRole 角色信息
	 * @return success/false
	 */
	@SysLog("修改角色")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys_role_edit')")
	public R update(@Valid @RequestBody SysRole sysRole) {
		return new R<>(sysRoleService.updateById(sysRole));
	}

	/**
	 * 删除角色
	 *
	 * @param id
	 * @return
	 */
	@SysLog("删除角色")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_role_del')")
	public R removeById(@PathVariable Integer id) {
		return new R<>(sysRoleService.removeRoleById(id));
	}

	/**
	 * 获取角色列表
	 *
	 * @return 角色列表
	 */
	@GetMapping("/list")
	public R listRoles() {
		return new R<>(sysRoleService.list(Wrappers.emptyWrapper()));
	}

	/**
	 * 分页查询角色信息
	 *
	 * @param page 分页对象
	 * @return 分页对象
	 */
	@GetMapping("/page")
	public R getRolePage(Page page) {
		return new R<>(sysRoleService.page(page, Wrappers.emptyWrapper()));
	}

	/**
	 * 更新角色菜单
	 *
	 * @param roleId  角色ID
	 * @param menuIds 菜单ID拼成的字符串，每个id之间根据逗号分隔
	 * @return success、false
	 */
	@SysLog("更新角色菜单")
	@PutMapping("/menu")
	@PreAuthorize("@pms.hasPermission('sys_role_perm')")
	public R saveRoleMenus(Integer roleId,
			@RequestParam(value = "menuIds", required = false) String menuIds) {
		SysRole sysRole = sysRoleService.getById(roleId);
		return new R<>(sysRoleMenuService.saveRoleMenus(sysRole.getRoleCode(), roleId, menuIds));
	}
}
